home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / Boolean.java < prev    next >
Text File  |  1998-09-22  |  6KB  |  181 lines

  1. /*
  2.  * @(#)Boolean.java    1.28 98/07/07
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.lang;
  16.  
  17. /**
  18.  * The Boolean class wraps a value of the primitive type 
  19.  * <code>boolean</code> in an object. An object of type 
  20.  * <code>Boolean</code> contains a single field whose type is 
  21.  * <code>boolean</code>. 
  22.  * <p>
  23.  * In addition, this class provides many methods for 
  24.  * converting a <code>boolean</code> to a <code>String</code> and a 
  25.  * <code>String</code> to a <code>boolean</code>, as well as other 
  26.  * constants and methods useful when dealing with a 
  27.  * <code>boolean</code>. 
  28.  *
  29.  * @author  Arthur van Hoff
  30.  * @version 1.28, 07/07/98
  31.  * @since   JDK1.0
  32.  */
  33. public final
  34. class Boolean implements java.io.Serializable {
  35.     /** 
  36.      * The <code>Boolean</code> object corresponding to the primitive 
  37.      * value <code>true</code>. 
  38.      *
  39.      * @since   JDK1.0
  40.      */
  41.     public static final Boolean TRUE = new Boolean(true);
  42.  
  43.     /** 
  44.      * The <code>Boolean</code> object corresponding to the primitive 
  45.      * value <code>false</code>. 
  46.      *
  47.      * @since   JDK1.0
  48.      */
  49.     public static final Boolean FALSE = new Boolean(false);
  50.  
  51.     /**
  52.      * The Class object representing the primitive type boolean.
  53.      *
  54.      * @since   JDK1.1
  55.      */
  56.     public static final Class    TYPE = Class.getPrimitiveClass("boolean");
  57.  
  58.     /**
  59.      * The value of the Boolean.
  60.      */
  61.     private boolean value;
  62.  
  63.     /** use serialVersionUID from JDK 1.0.2 for interoperability */
  64.     private static final long serialVersionUID = -3665804199014368530L;
  65.  
  66.     /**
  67.      * Allocates a <code>Boolean</code> object representing the 
  68.      * <code>value</code> argument. 
  69.      *
  70.      * @param   value   the value of the <code>Boolean</code>.
  71.      * @since   JDK1.0
  72.      */
  73.     public Boolean(boolean value) {
  74.     this.value = value;
  75.     }
  76.  
  77.     /**
  78.      * Allocates a <code>Boolean</code> object representing the value 
  79.      * <code>true</code> if the string argument is not <code>null</code> 
  80.      * and is equal, ignoring case, to the string <code>"true"</code>. 
  81.      * Otherwise, allocate a <code>Boolean</code> object representing the 
  82.      * value <code>false</code>. 
  83.      *
  84.      * @param   s   the string to be converted to a <code>Boolean</code>.
  85.      * @since   JDK1.0
  86.      */
  87.     public Boolean(String s) {
  88.     this(toBoolean(s));
  89.     }
  90.  
  91.     /**
  92.      * Returns the value of this Boolean object as a boolean.
  93.      *
  94.      * @return  the primitive <code>boolean</code> value of this object.
  95.      * @since   JDK1.0
  96.      */
  97.     public boolean booleanValue() {
  98.     return value;
  99.     }
  100.  
  101.     /**
  102.      * Returns the boolean value represented by the specified String.
  103.      * A new <code>Boolean</code> object is constructed. This 
  104.      * <code>Boolean</code> contains the value <code>true</code> if the 
  105.      * string argument is not <code>null</code> and is equal, ignoring 
  106.      * case, to the string <code>"true"</code>. 
  107.      *
  108.      * @param   s   a string.
  109.      * @return  the <code>Boolean</code> value represented by the string.
  110.      * @since   JDK1.0
  111.      */
  112.     public static Boolean valueOf(String s) {
  113.     return new Boolean(toBoolean(s));
  114.     }
  115.  
  116.     /**
  117.      * Returns a String object representing this Boolean's value.
  118.      * If this object contains the value <code>true</code>, a string equal 
  119.      * to <code>"true"</code> is returned. Otherwise, a string equal to 
  120.      * <code>"false"</code> is returned. 
  121.      *
  122.      * @return  a string representation of this object. 
  123.      * @since   JDK1.0
  124.      */
  125.     public String toString() {
  126.     return value ? "true" : "false";
  127.     }
  128.  
  129.     /**
  130.      * Returns a hash code for this Boolean.
  131.      *
  132.      * @return  a hash code value for this object.
  133.      * @since   JDK1.0
  134.      */
  135.     public int hashCode() {
  136.     return value ? 1231 : 1237;
  137.     }
  138.  
  139.     /**
  140.      * Returns <code>true</code> if and only if the argument is not 
  141.      * <code>null</code> and is a <code>Boolean </code>object that 
  142.      * contains the same <code>boolean</code> value as this object. 
  143.      *
  144.      * @param   obj   the object to compare with.
  145.      * @return  <code>true</code> if the objects are the same;
  146.      *          <code>false</code> otherwise.
  147.      * @since   JDK1.0
  148.      */
  149.     public boolean equals(Object obj) {
  150.     if ((obj != null) && (obj instanceof Boolean)) {
  151.         return value == ((Boolean)obj).booleanValue();
  152.     } 
  153.     return false;
  154.     }
  155.  
  156.     /**
  157.      * Returns is <code>true</code> if and only if the system property 
  158.      * named by the argument exists and is equal to the string 
  159.      * <code>"true"</code>. (Beginning with Java 1.0.2, the test of 
  160.      * this string is case insensitive.) A system property is accessible 
  161.      * through <code>getProperty</code>, a method defined by the 
  162.      * <code>System</code> class. 
  163.      *
  164.      * @param   name   the system property name.
  165.      * @return  the <code>boolean</code> value of the system property.
  166.      * @see     java.lang.System#getProperty(java.lang.String)
  167.      * @see     java.lang.System#getProperty(java.lang.String, java.lang.String)
  168.      * @since   JDK1.0
  169.      */
  170.     public static boolean getBoolean(String name) {
  171.     SecurityManager sm = System.getSecurityManager();
  172.     if (sm != null)
  173.         sm.checkPropertyAccess(name);
  174.     return toBoolean(System.getProperty(name));
  175.     }
  176.  
  177.     private static boolean toBoolean(String name) { 
  178.     return ((name != null) && name.toLowerCase().equals("true"));
  179.     }
  180. }
  181.